package fixtures;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.naming.spi.DirStateFactory.Result;

import com.mysql.jdbc.ResultSet;

import server.SQLConnection;
import Messages.MessageNewEmail;
import Messages.MessageNewTelephone;
import fit.ActionFixture;



public class EditDetails extends ActionFixture   {
	
	

	private String newtelephone,id; 
	public String[] str;
	private ResultSet rs;
	public Statement stmt;
	public SQLConnection conn;
	private String Email,NewTelephone;
	
	public MessageNewTelephone t=new MessageNewTelephone();
	public MessageNewEmail newEmail= new MessageNewEmail();
	
	public void edittelephone(String phoneNum) throws SQLException, IOException {
		stmt = conn.getConnection().createStatement();
		java.sql.ResultSet rs1= stmt.executeQuery("select * from db1.subscriber ");
		rs1.next();
		id=rs1.getString(1);
		
		java.sql.ResultSet rs2= stmt.executeQuery("select * from db1.subscriber where ID = '"+id+"' ");
		rs2.next();
		
		NewTelephone=phoneNum;
		
		if (Telcorrectness())
		{
	    String[] str2= {"123", rs2.getString(1),phoneNum};
	    t.edittelephone(str2);	
		}
	}
	
	
	public String checkeditTel() throws SQLException, IOException {
		
	
		java.sql.ResultSet rs2= stmt.executeQuery("select * from db1.subscriber where ID = '"+id+"' ");
		rs2.next();
		
		
	   return rs2.getString(4);
	}
	
	
	public boolean startcon(){
		
	
		conn = new SQLConnection();
		return true;
		
	}
	
	public void editEmail(String email) throws SQLException, IOException {
	
		
		java.sql.ResultSet rs2= stmt.executeQuery("select * from db1.subscriber where ID = '"+id+"' ");
		rs2.next();
		
		Email= email;
		
		if (emailcorrectness())
		{
	    String[] str2= {"123", rs2.getString(1),email};
	    newEmail.editmail(str2);	
		}
		
	
		
		
	}


	public boolean emailcorrectness() {
		Pattern p = Pattern.compile("@");
		Matcher m = p.matcher(Email);
		if  (!m.find()) return false; 
		else return true;
	}


	public String checkmail() throws SQLException, IOException {
		
		
		java.sql.ResultSet rs2= stmt.executeQuery("select * from db1.subscriber where ID = '"+id+"' ");
		rs2.next();
		
		
	   return rs2.getString(2);
	}
	
	
	
	

		
		public boolean Telcorrectness() {
		Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^-]");
		Matcher m = p.matcher(NewTelephone);
		if  (m.find()) return false; 
		else return true;
	}
	

	


}
	
	
	
	
	
	
